package com.xindacloud.sms.adapter.controller;

import com.xindacloud.sms.adapter.component.BasicInsight;
import com.xindacloud.sms.adapter.dto.SearchMessageDTO;
import com.xindacloud.sms.adapter.dto.SubmitMessageParamDTO;
import com.xindacloud.sms.adapter.dto.ValidNumberParamDTO;
import com.xindacloud.sms.adapter.service.business.ISmsSubmissionRecordService;
import com.xindacloud.sms.adapter.service.notify.NotifyExecutor;
import com.xindacloud.sms.adapter.utils.MessageTypeHelp;
import com.xindacloud.sms.adapter.utils.UnionResultHelp;
import com.xindacloud.sms.adapter.vo.SubmitMessageResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Api(tags = {"短信发送接口"})
@RestController
@RequestMapping(value = "/api/v1/adapter/sms")
@Slf4j
public class SubmitMessageController {


    private final ISmsSubmissionRecordService smsSubmissionRecordService;

    private final NotifyExecutor notifyExecutor;

    @Autowired
    BasicInsight basicInsight;

    @Autowired
    public SubmitMessageController(ISmsSubmissionRecordService smsSubmissionRecordService, NotifyExecutor notifyExecutor) {
        this.smsSubmissionRecordService = smsSubmissionRecordService;
        this.notifyExecutor = notifyExecutor;
    }


    @ApiOperation(value = "提交短信至队列", response = UnionResultHelp.class)
    @PostMapping(value = "/submit", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public UnionResultHelp submitMessage(@ApiParam(name = "submitMessageParamDTO", value = "提交短信DTO", required = true)
                                         @RequestBody SubmitMessageParamDTO submitMessageParamDTO,
                                         @ApiParam(name = "username", value = "用户名", required = true)
                                         @RequestHeader(name = "username") String username,
                                         @ApiParam(name = "password", value = "密码", required = true)
                                         @RequestHeader(name = "password") String password) {
        try {

            log.info("####提交短信至队列开始时间{}:", UnionResultHelp.getStrTimestamp());
            final String encoding = MessageTypeHelp.isUnicode(submitMessageParamDTO.getMessageBody())
                    ? MessageTypeHelp.MessageTypeEnum.UNICODE.toString()
                    : MessageTypeHelp.MessageTypeEnum.GSM.toString();
            submitMessageParamDTO.setEncoding(encoding);
            submitMessageParamDTO.setMessageCount(MessageTypeHelp.calcMessageCount(submitMessageParamDTO.getMessageBody()));
            notifyExecutor.sendNotifyMessage(submitMessageParamDTO, username, password);
            log.info("####提交短信至队列完毕结束时间:{}", UnionResultHelp.getStrTimestamp());
            return new UnionResultHelp(UnionResultHelp.StatusEnum.Success, "提交短信成功.");
        } catch (Exception e) {
            log.error("提交短信异常:{}", e.getMessage(), e);
        }
        return new UnionResultHelp(UnionResultHelp.StatusEnum.Fail, "提交短信异常.");
    }

    @ApiOperation(value = "短信查询", response = SubmitMessageResultVO.class)
    @PostMapping(value = "/search", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object searchMessage(@RequestBody SearchMessageDTO searchMessageDTO) {
        return smsSubmissionRecordService.searchMessage(searchMessageDTO);
    }

    @ApiOperation(value = "号码校验", response = List.class)
    @PostMapping(value = "/valid-number", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object validNumber(@RequestBody ValidNumberParamDTO validNumberParamDTO,
                              @ApiParam(name = "username", value = "用户名", required = true)
                              @RequestHeader(name = "username") String username,
                              @ApiParam(name = "password", value = "密码", required = true)
                              @RequestHeader(name = "password") String password) {
        return basicInsight.validNumber(new ArrayList<>(new HashSet<>(validNumberParamDTO.getRecipientList())));
    }

}
